package com.nuo.backend.modules.test.interceptor;

import com.nuo.backend.common.threadlocal.RequestBodyThreadLocal;
import com.nuo.backend.common.utils.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description: TODO 获取请求体
 * @author nuo
 * @date 2022/12/3 15:57
 * @version 1.0
 */
@Component
@Slf4j
public class RequestBodyInterceptor implements HandlerInterceptor {

    @Autowired
    private RequestBodyThreadLocal requestBodyThreadLocal;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("enter RequestBodyInterceptor");
        requestBodyThreadLocal.set(SpringContextUtils.getRequestBodyJson(request));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
